<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view' );
class PropertiesViewPanel extends JView
{
	function display($tpl = null)
	{
		global $mainframe;
		$user =& JFactory::getUser();
		$task = JRequest::getVar('task');
		$layout = JRequest::getVar('layout');

		if (!$user->guest) {

			$component = JComponentHelper::getComponent( 'com_properties' );
			$params = new JParameter( $component->params );
			$Itemid =& $this->get('Itemid');
			$this->assignRef('Itemid',		$Itemid);

			switch($task)
			{
				case 'edit':
					$layout='addproperty';

					$ProductEdit		=& $this->get('ProductEdit');
					$this->assignRef('datos',		$ProductEdit);

					//parent::display($layout);
					break;

				case 'new':
					$layout='addproperty';
					//parent::display($layout);
					break;

				default:
					$items		= & $this->get( 'DataPanel');
					$pagination =& $this->get('PaginationPanel');
					$DataThisAgent =  & $this->get( 'Profile');
					$Cant_items =  & $this->get( 'Cant_items');
					$this->assignRef('items',		$items);
					$this->assignRef('pagination', $pagination);
					$this->assignRef('DataThisAgent',		$DataThisAgent);
					$this->assignRef('Cant_items',		$Cant_items);
					break;
			}


			$NameCategory			= $this->NameCategory();
			$NameType				=& $this->NameType();
			$NameCountry			= $this->NameCountry();
			$NameState				=& $this->NameState();
			$NameLocality			=& $this->NameLocality();
			$this->assignRef('NameCategory',	$NameCategory);
			$this->assignRef('NameType',		$NameType);
			$this->assignRef('NameCountry',		$NameCountry);
			$this->assignRef('NameState',		$NameState);
			$this->assignRef('NameLocality',	$NameLocality);

			parent::display($layout);




		}else{

			$Retornar='index.php?option=com_properties&view=panel&Itemid='.$Itemid;

			$link = JRoute::_( 'index.php?option=com_user&view=login' );
			$msg = JText::_( 'Registered Users only can acces' );
			//$mainframe->Redirect($link, $msg);

			global $mainframe, $option;

			// Initialize variables
			$document	=& JFactory::getDocument();
			$user		=& JFactory::getUser();
			$pathway	=& $mainframe->getPathway();
			$image		= '';

			$menu   =& JSite::getMenu();
			$item   = $menu->getActive();
			if($item)
			$params	=& $menu->getParams($item->id);
			else
			$params	=& $menu->getParams(null);


			$type = (!$user->get('guest')) ? 'logout' : 'login';

			// Set some default page parameters if not set
			$params->def( 'page_title', 				1 );
			if(!$item)
			{
				$params->def( 'header_login', 			'' );
				$params->def( 'header_logout', 			'' );
			}

			$params->def( 'pageclass_sfx', 			'' );
			$params->def( 'login', 					'index.php' );
			$params->def( 'logout', 				'index.php' );
			$params->def( 'description_login', 		1 );
			$params->def( 'description_logout', 		1 );
			$params->def( 'description_login_text', 	JText::_( 'LOGIN_DESCRIPTION' ) );
			$params->def( 'description_logout_text',	JText::_( 'LOGOUT_DESCRIPTION' ) );
			$params->def( 'image_login', 				'key.jpg' );
			$params->def( 'image_logout', 				'key.jpg' );
			$params->def( 'image_login_align', 			'right' );
			$params->def( 'image_logout_align', 		'right' );
			$usersConfig = &JComponentHelper::getParams( 'com_users' );
			$params->def( 'registration', 				$usersConfig->get( 'allowUserRegistration' ) );

			if ( !$user->get('guest') )
			{
				$title = JText::_( 'Logout');

				// pathway item
				$pathway->addItem($title, '' );
				// Set page title
				$document->setTitle( $title );
			}
			else
			{
				$title = JText::_( 'Login');

				// pathway item
				$pathway->addItem($title, '' );
				// Set page title
				$document->setTitle( $title );
			}

			// Build login image if enabled
			if ( $params->get( 'image_'.$type ) != -1 ) {
				$image = 'images/stories/'.$params->get( 'image_'.$type );
				$image = '<img src="'. $image  .'" align="'. $params->get( 'image_'.$type.'_align' ) .'" hspace="10" alt="" />';
			}

			// Get the return URL
			if (!$url = JRequest::getVar('return', '', 'method', 'base64')) {
				$url = base64_encode($Retornar);
			}

			$errors =& JError::getErrors();

			$this->assign('image' , $image);
			$this->assign('type'  , $type);
			$this->assign('return', $url);
			$this->assignRef('params', $params);




			if($task!='login'){

				$v=JRequest::getVar('view');
				$l=JRequest::getVar('layout');
				if($l){
					$Retornar='index.php?option=com_properties&view=panel&layout=myprofile&Itemid='.$Itemid;
					$url = base64_encode($Retornar);
				}else{
					$Retornar='index.php?option=com_properties&view=panel&Itemid='.$Itemid;
					$url = base64_encode($Retornar);
				}

				$link = 'index.php?option=com_properties&view=panel&task=login&return='.$url ;
				$msg = JText::_( 'Registered Users only can access' );
				$mainframe->Redirect($link, $msg);
			}


			parent::display('login');
		}


	}








	function NameCategory()
	{
		$db 	=& JFactory::getDBO();
		$query = 'SELECT * FROM #__properties_category WHERE published = 1 ORDER BY id';
		$db->setQuery($query);
		$Categories = $db->loadObjectList();

		foreach ($Categories as $row) :
		$Category[$row->id] = $row->name;
		endforeach;

		return $Category;
	}

	function NameType()
	{
		$db 	=& JFactory::getDBO();
		$query = 'SELECT * FROM #__properties_type WHERE published = 1 ORDER BY name';
		$db->setQuery($query);
		$Types = $db->loadObjectList();

		foreach ($Types as $row) :
		$Type[$row->id] = $row->name;
		endforeach;

			
		return $Type;
	}

	function NameCountry()
	{
		$db 	=& JFactory::getDBO();
		$query = 'SELECT * FROM #__properties_country WHERE published = 1 ORDER BY name';
		$db->setQuery($query);
		$Countries = $db->loadObjectList();
			
		foreach ($Countries as $row) :
		$Country[$row->id] = $row->name;
		endforeach;

		return $Country;
	}
	function NameState()
	{
		$db 	=& JFactory::getDBO();
		$query = 'SELECT * FROM #__properties_state WHERE published = 1 ORDER BY name';
		$db->setQuery($query);
		$States = $db->loadObjectList();

		foreach ($States as $row) :
		$State[$row->id] = $row->name;
		endforeach;

		return $State;
	}
	function NameLocality()
	{
		$db 	=& JFactory::getDBO();
		$query = 'SELECT * FROM #__properties_locality WHERE published = 1 ORDER BY name';
		$db->setQuery($query);
		$Localities = $db->loadObjectList();

		foreach ($Localities as $row) :
		$Locality[$row->id] = $row->name;
		endforeach;

		return $Locality;
	}
}


